<?php
namespace app\index\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use qrcode;
class IndexController extends Controller
{
    public function index()
    {
        $brand_list = Db::name('brand')->select();
        // dump($brand_list);
        // exit;
    	// $phone = Session::get('member_phone');
    	// $seller_id = Session::get('seller_name');
    	// // dump($seller_id);
    	$this->assign('brand_list',$brand_list);
    	// $this->assign('seller_id',$seller_id);
       	return $this->fetch('index');
    }



    public function generate()
    {

    	$phone = Session::get('member_phone');
    	$seller_id = Session::get('seller_name');
    	$sid = Session::get('seller_id');
    	$seller_list = Db::name('seller')
        ->alias('b')
        ->join('drm_selling w','b.selling_id = w.selling_id')
        ->where(['seller_id'=>$sid])
        ->find();
        // dump($seller_list);
        // exit;
        $brandList = explode(",",$seller_list['b_id']);
        $brandsring = '';
        foreach ($brandList as $key => $value) {
            $brandsring .= "`brand_id`="."'".$value."'"." or ";

        }
        $brandsring = rtrim($brandsring,'or ');
        $brandArr = db('brand')->where($brandsring)->select();
 
        
        // dump($brandList);
        // exit;
        $m_name = $seller_list['selling_name'];
        // $sellerList[] = $seller_list;
    	// $seller_list = Db::name('seller')->where(['seller'=>$sid])->find();
    	// $seller_n
    	// dump($seller_list);
    	// exit;
    	$this->assign('phone',$phone);
    	$this->assign('seller_id',$seller_id);
        $this->assign('m_name',$m_name);
    	$this->assign('brandArr',$brandArr);
    	return $this->fetch();
    }











    public function er_code()
    {

        
        $list = input('post.');

        $sid = Session::get('seller_id');
        
        $number = rand(10000,99999).time();
        $arr = [];
        $count = null;
        $num = null;
        foreach ($list['data'] as $key => $value) {
            $arr[$key]= $value;
            $arr[$key]['number']= $number;

            // $count += $value['unit'];
            // $count['unit'] = $value['unit']++;
            // $res[]= array_filter($value);
            // $res[]['selling'] = input('post.selling');
            if (empty($value['goods_name'])) {
                unset($arr[$key]);
            }
        }

        // dump($arr);
        // exit;
        foreach ($arr as $key => $value) {
            $count += $value['unit']*$value['num'];
            // $num += $value['num'];
        }
        $phone = $list['member_phone'];
        $member_list = db('member')->where(['phone'=>$phone])->find();
        $order['number'] = $number;
        $order['selling_name'] = input('post.selling_name');
        $order['selling_user'] = input('post.selling_user');
        $order['selling_phone'] = input('post.selling_phone');
        $order['d_phone'] = input('post.d_phone');
        $order['user_name'] = input('post.user_name');
        $order['user_phone'] = input('post.user_phone');
        $order['time'] = input('post.time');
        $order['j_time'] = input('post.j_time');
        $order['member_phone'] = input('post.member_phone');
        $order['user_address'] = input('post.user_address');
        $order['freight'] = input('post.freight');
        $order['total'] = $count;
        $order['seller_id'] = $sid;
        $order['uid'] = $member_list['uid'];
        
       
        //  dump($brandList);
        
        // insertGetId
        $order_id = db('goods_order')->insertGetId($order);
        
        $goods_list = [];
        if (!empty($order_id)) {
            foreach ($arr as $key => $value) {
                $goods_list[$key]= $value;
                $brandList = db('brand')->where(['brand_name'=>$value['brand_name']])->select();
                foreach ($brandList as $k => $v) {
                    if ($goods_list[$key]['brand_name'] == $v['brand_name']) {
                        $goods_list[$key]['goods_img'] = $v['brand_img'];
                        $goods_list[$key]['order_id']= $order_id;
                        $goods_list[$key]['uid']= $member_list['uid'];
                        
                    }
                }
                
            }
         
            // foreach ($arr as $key => $value) {
            //     $goods_list[$key]= $value;
            //     $goods_list[$key]['order_id']= $order_id;
            //     $goods_list[$key]['uid']= $member_list['uid'];
            // }
            if (db('goods')->insertAll($goods_list) > 0) {
                echo json_encode($number);
                exit;
            }else{
                return 'error';
            }
        }else{
            return 'error';
        }

        
        // dump($order);
        // dump($arr);
        // exit;
        // // dump($)
        // // echo $num;
        // // echo $count;
        // // exit;
        // // dump($arr);
        // // exit;
        // $res = [];
        // foreach ($arr as $key => $value) {
        //     $res[$key] = $value;
        //     $res[$key]['number'] = $number;
        //     $res[$key]['selling_name'] = input('post.selling_name');
        //     $res[$key]['selling_user'] = input('post.selling_user');
        //     $res[$key]['selling_phone'] = input('post.selling_phone');
        //     $res[$key]['d_phone'] = input('post.d_phone');
        //     $res[$key]['user_name'] = input('post.user_name');
        //     $res[$key]['user_phone'] = input('post.user_phone');
        //     $res[$key]['time'] = input('post.time');
        //     $res[$key]['j_time'] = input('post.j_time');
        //     $res[$key]['member_phone'] = input('post.member_phone');
        //     $res[$key]['user_address'] = input('post.user_address');
        //     $res[$key]['freight'] = input('post.freight');
        //     $res[$key]['total'] = $count;
        //     $res[$key]['seller_id'] = $sid;
        //     $res[$key]['uid'] = $member_list['uid'];
            
        // }
       
        // // dump($res);
        // // exit;
        // // $order_id = db('goods_order')->insertAll ($res);
        // // dump($order_id);
        // if (db('goods_order')->insertAll ($res) != '0') {
        //     echo json_encode($number);
        //     exit;
        // }else{
        //     return 'error';
        // }
        // include_once ROOT_PATH.'extend/phpqrcode/qrlib.php';
        // QRcode::png('https://www.baidu.com',false,'OR_ECLEVEL_L',10);
        // exit;
        
    }


    public function love()
    {
        return $this->fetch();
    }
}
